package br.eng.eliseu.gwt.aconchego.camera.server;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import br.eng.eliseu.gwt.aconchego.camera.module.Camera;
import br.eng.eliseu.gwt.aconchego.camera.module.StatusCamEnum;
import br.eng.eliseu.gwt.aconchego.camera.server.service.UsuarioServiceImpl;

public class InicioContextListener implements ServletContextListener{

	public void contextInitialized(ServletContextEvent arg0) {
		UsuarioServiceImpl usuarioImpl = new UsuarioServiceImpl();
		try {
			List<Camera> lista = usuarioImpl.getCameras();
			if (lista.size()<= 0){
				usuarioImpl.gravaCamera(new Camera("Camera_01", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_02", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_03", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_04", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_05", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_06", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_07", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_08", "", StatusCamEnum.ATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_09", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_10", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_11", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_12", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_13", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_14", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_15", "", StatusCamEnum.DESATIVADA));
				usuarioImpl.gravaCamera(new Camera("Camera_16", "", StatusCamEnum.DESATIVADA));
			} else {
				int nroCameras = 8;
				/* Refaco as configuracoes ja existente todas as vezes
				 * Assim, posso alterar as configuracoes por aqui sempre que for preciso
				 */
				for (int i=0;i<nroCameras;i++) {
					Camera c = new Camera();
					if (i<lista.size()){
						c = lista.get(i);
					} else {
						// --- Sempre que uma nova camera for acrescentada
						c.setId(i+1);
					}

					if (c.getId()==1){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/ipcam/bercario1ipcam/&");
						cmd.append("file=bercario1ipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/bercario1ipcam&");
						c.setComando(cmd.toString());
						c.setNome("Berçário 1");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==2){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/ipcam/bercario2ipcam/&");
						cmd.append("file=bercario2ipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/bercario2ipcam&");
						c.setComando(cmd.toString());
						c.setNome("Berçário 2");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==3){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/ipcam/bercario3ipcam/&");
						cmd.append("file=bercario3ipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/bercario3ipcam&");
						c.setComando(cmd.toString());
						c.setNome("Berçário 3");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==4){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/ipcam/minimaternalipcam/&");
						cmd.append("file=minimaternalipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/minimaternalipcam&");
						c.setComando(cmd.toString());
						c.setNome("Mini Maternal");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==5){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/players/aconchegoipcam/bercarioipcam/&");
						cmd.append("file=bercarioipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/bercarioipcam/&");
						c.setComando(cmd.toString());
						c.setNome("Maternal");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==6){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/ipcam/brinquedotecaipcam/&");
						cmd.append("file=brinquedotecaipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/brinquedotecaipcam&");
						c.setComando(cmd.toString());
						c.setNome("Brinquedoteca");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==7){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/players/aconchegoipcam/churrasqueiraipcam/&");
						cmd.append("file=churrasqueiraipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/churrasqueiraipcam/&");
						c.setComando(cmd.toString());
						c.setNome("Refeitório");
						c.setStatus(StatusCamEnum.ATIVADA);
					} else if (c.getId()==8){
						StringBuffer cmd = new StringBuffer();
						cmd.append("netstreambasepath=http://38.96.175.106/players/aconchegoipcam/refeitorioipcam/&");
						cmd.append("file=refeitorioipcam.stream&");
						cmd.append("streamer=rtmp://38.96.175.108/refeitorioipcam/&");
						c.setComando(cmd.toString());
						c.setNome("Solário");
						c.setStatus(StatusCamEnum.ATIVADA);
					}
					
					usuarioImpl.gravaCamera(c);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
